package saobei

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/go-pay/gopay"
)

//资金接口 https://help.lcsw.cn/xrmpic/tisnldchblgxohfl/ml9uvg

// WithdrawQueryCash 查询余额 https://help.lcsw.cn/xrmpic/tisnldchblgxohfl/ml9uvg#title-node1
func (c *Client) WithdrawQueryCash(ctx context.Context, bm gopay.BodyMap) (rsp *DoAllocateRsp, err error) {
	err = bm.CheckEmptyError("trace_no")
	if err != nil {
		return nil, err
	}
	var bs []byte
	if bs, err = c.doWithdrawPost(ctx, withdrawQueryCashPath, bm); err != nil {
		return nil, err
	}
	rsp = new(DoAllocateRsp)
	if err = json.Unmarshal(bs, rsp); err != nil {
		return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
	}
	if err := bizErrCheck(rsp.RspBase); err != nil {
		return nil, err
	}
	return rsp, c.verifySign(bs)
}

// WithdrawQueryFee 查询提现手续费 https://help.lcsw.cn/xrmpic/tisnldchblgxohfl/ml9uvg#title-node2
func (c *Client) WithdrawQueryFee(ctx context.Context, bm gopay.BodyMap) (rsp *WithdrawQueryFeeRsp, err error) {
	err = bm.CheckEmptyError("trace_no", "amt", "zero_arr_type")
	if err != nil {
		return nil, err
	}
	var bs []byte
	if bs, err = c.doWithdrawPost(ctx, withdrawQueryFeePath, bm); err != nil {
		return nil, err
	}
	rsp = new(WithdrawQueryFeeRsp)
	if err = json.Unmarshal(bs, rsp); err != nil {
		return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
	}
	if err := bizErrCheck(rsp.RspBase); err != nil {
		return nil, err
	}
	return rsp, c.verifySign(bs)
}

// WithdrawApplyCash 发起提现 https://help.lcsw.cn/xrmpic/tisnldchblgxohfl/ml9uvg#title-node3
func (c *Client) WithdrawApplyCash(ctx context.Context, bm gopay.BodyMap) (rsp *WithdrawApplyCashRsp, err error) {
	err = bm.CheckEmptyError("trace_no", "amt", "fee_amt", "zero_arr_type")
	if err != nil {
		return nil, err
	}
	var bs []byte
	if bs, err = c.doWithdrawPost(ctx, withdrawApplyCashPath, bm); err != nil {
		return nil, err
	}
	rsp = new(WithdrawApplyCashRsp)
	if err = json.Unmarshal(bs, rsp); err != nil {
		return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
	}
	if err := bizErrCheck(rsp.RspBase); err != nil {
		return nil, err
	}
	return rsp, c.verifySign(bs)
}
